package thecardgame;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Game {
    public static final String[] SUIT = {"❤", "♠", "♦", "♣"};

    //创建一套纸牌
    public List<Card> createCards() {
        List<Card> cardsList = new ArrayList<>();//纸牌用顺序表存储
        for (int i = 0; i < SUIT.length; i++) {
            for (int j = 0; j <= 13; j++) {
                Card card = new Card(SUIT[i], j);
                cardsList.add(card);
            }
        }
        return cardsList;
    }

    //洗牌
    public void shuffle(List<Card> cardList) {
        Random random = new Random();
        for (int i = cardList.size() - 1; i > 0; i--) {
            int ranIndex = random.nextInt(i);
            swap(cardList, i, ranIndex);
        }
    }
    private void swap(List<Card> cardList, int i, int j) {
        Card tmp = cardList.get(i);
        cardList.set(i, cardList.get(j));
        cardList.set(j, tmp);
    }

    /**
     * 玩牌：三人轮流抓牌，每人五张牌
     */
    public List<List<Card>> play(List<Card> cardList) {
        List<List<Card>> hand = new ArrayList<>();

        List<Card> hand0 = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();

        hand.add(hand0);
        hand.add(hand1);
        hand.add(hand2);

        //每人轮流拿五张牌
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardList.remove(0);
                hand.get(j).add(card);
            }
        }
        return hand;
    }
}
